﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.ActiveXPartLib
{

    /// <summary>
    /// 对单程序多窗口进行处理
    /// 主要针对视频组件
    /// </summary>
    class INSVideoProgramWindowHost : System.Windows.Controls.UserControl
    {
        public struct Rect
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        #region win 32
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);

        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
         CharSet = CharSet.Unicode, ExactSpelling = true,
         CallingConvention = CallingConvention.StdCall)]
        private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
        private static extern long GetWindowLong(IntPtr hwnd, int nIndex);
        [DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
        private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags);
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam);
        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        private const int SWP_NOOWNERZORDER = 0x200;
        private const int SWP_NOREDRAW = 0x8;
        private const int SWP_NOZORDER = 0x4;
        private const int SWP_SHOWWINDOW = 0x0040;
        private const int WS_EX_MDICHILD = 0x40;
        private const int SWP_FRAMECHANGED = 0x20;
        private const int SWP_NOACTIVATE = 0x10;
        private const int SWP_ASYNCWINDOWPOS = 0x4000;
        private const int SWP_NOMOVE = 0x2;
        private const int SWP_NOSIZE = 0x1;
        private const int GWL_STYLE = (-16);
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CLOSE = 0x10;
        private const int WS_CHILD = 0x40000000;

        public const int SW_HIDE = 0;
        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;
        public const int SW_SHOWMAXIMIZED = 3;
        public const int SW_MAXIMIZE = 3;
        public const int SW_SHOWNOACTIVATE = 4;
        public const int SW_SHOW = 5;
        public const int SW_MINIMIZE = 6;
        public const int SW_SHOWMINNOACTIVE = 7;
        public const int SW_SHOWNA = 8;
        public const int SW_RESTORE = 9;

        #endregion

        public IntPtr appWin;
        SingletonINSVideoPro s_pro;
        System.Windows.Forms.Timer _timer;

        System.Drawing.Point _position;
        System.Drawing.Size _size;

        public void Start(SingletonINSVideoPro pro, IntPtr hwnd)
        {
            s_pro = pro;
            Common.Logger.DebugToTag("UI", string.Format("SingletonINSVideoPro Start:hwnd", hwnd));
            appWin = hwnd;

            if (_timer != null)
            {
                _timer.Stop();
                _timer = null;
            }
            _timer = new System.Windows.Forms.Timer();
            _timer.Tick += _timer_Tick;
            _timer.Interval = 100;
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            if (appWin != IntPtr.Zero)
            {
                System.Windows.Point pt = default(System.Windows.Point);
                try
                {
                    pt = this.PointToScreen(new System.Windows.Point(0, 0));
                }
                catch (Exception ex)
                {
                    Common.Logger.DebugToTag("UI", string.Format("_timer_Tick出错,ex:{0}", ex));
                    return;
                }
                Rect rect;
                GetWindowRect(appWin, out rect);

                int curX = (int)pt.X;
                int curY = (int)pt.Y;
                int curWidth = (int)this.ActualWidth;
                int curHeight = (int)this.ActualHeight;
                if (curX != _position.X
                    || curY != _position.Y
                    || curWidth != _size.Width
                    || curHeight != _size.Height
                    || rect.Left != curX
                    || rect.Top != curY)
                {
                    MoveWindow(appWin, curX, curY,
                        curWidth, curHeight, false);
                    //SwitchToThisWindow(appWin, true);
                    //SetWindowPos(appWin, -1, pt.X, pt.Y, this.Width, this.Height, 2);
                    Common.Logger.DebugToTag("UI", string.Format("move window, hwnd:{0}, x:{1},y:{2},w:{3},h:{4}, rect:{5},{6},{7},{8}", appWin,
                        pt.X,
                        pt.Y,
                        curWidth,
                        curHeight,
                        rect.Left,
                        rect.Top,
                        rect.Right,
                        rect.Bottom));

                    _position.X = curX;
                    _position.Y = curY;
                    _size.Width = curWidth;
                    _size.Height = curHeight;
                }
            }
        }

        public void Stop()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer = null;
            }

            if (appWin != null)
            {
                MoveWindow(appWin, 0, 0,
                    0, 0, false);
            }
        }
    }
}
